<template>
  <div class="register-container">
  <div class="main-content">
  <p class="register-intro">一共有2000名用户注册了本网站，30s快速注册，等你来约~</p>
    <el-form autoComplete="on" :model="registerForm" :rules="registerRules" ref="registerForm" label-position="left" label-width="0px"
      class="card-box register-form">
      
      <div class="register-header">
        <span class="left">
          注册
        </span>
        <span class="">
          更快一步找到他（她）
        </span>
        <i class="el-dialog__close el-icon el-icon-close right">
        </i>
      </div>
       <el-form-item prop="username">
        <span class="svg-container svg-container_register">
          用户名：
        </span>
        <el-input name="username" type="text" v-model="registerForm.username" autoComplete="on" placeholder="用户名" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container svg-container_register">
          密码：
        </span>
        <el-input name="password" type="password" @keyup.enter.native="handleregister" v-model="registerForm.password" autoComplete="on"
          placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item prop="testCode">
        <span class="svg-container svg-container_register">
          验证码：
        </span>
        <el-input name="testCode" type="text" style="width:20%;" @keyup.enter.native="handleRegister" v-model="registerForm.testCode" autoComplete="on"
          placeholder="验证码"></el-input>
          <el-input name="code" type="text" style="width:15%;" v-model="registerForm.code" autoComplete="on"
          placeholder="1234" disabled="disabled"></el-input>
           <el-button style="width:15%;">点击刷新</el-button>
      </el-form-item>
      <el-form-item style="padding:0 26% 0 30%;">
        <el-button type="danger" class="left" style="padding:6px 0;" :loading="loading" @click.native.prevent="handleRegister">
          登录
        </el-button>
        <el-button type="info" class="right" style="padding:6px 0;" :loading="loading" @click.native.prevent="handleRegister">
          注册
        </el-button>
      </el-form-item>
       <el-form-item style="padding:0 23% 0 30%;">
        <el-button class="right" :loading="loading" @click.native.prevent="handleRegister">
          忘记密码？
        </el-button>
      </el-form-item>
    </el-form>
    </div>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'

export default {
  name: 'register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名 --来自系统的侮辱'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位 --来自系统的侮辱'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        username: 'admin',
        password: '111111',
        code:'7878',
        testCode:''
      },
      registerRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false
    }
  },
  methods: {
    handleRegister() {
      // 获取ref="registerForm"的数据
      if(this.registerForm.testCode!=this.registerForm.code){
        this.$message({
              type: 'info',
              message: `请输入正确的验证码`
            });
      }else{
         this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Register', this.registerForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
      }
      
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  @import "src/styles/mixin.scss";
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .register-container {
    @include relative;
    height: 100vh;
    background-color: #fff;
    font-size:.875rem;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
      -webkit-text-fill-color: #fff !important;
    }
    form>div:nth-child(2) {
    margin-top: 30px;
}
.register-intro{
    text-align:center;
    margin-top:100px;
    height:20px;
}
.main-content{
    width: 376px;
    height:300px;
    margin:120px auto;
}
.el-form-item__error {
    color: #ff4949;
    font-size: 12px;
    line-height: 1;
    padding-top: 0; 
    position: absolute;
    top: 100%;
    left: 30%;
}
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 0 15px;
      color: $light_gray;
    }
    .el-input {
      display: inline-block;
      width: 50%;
      border:1px solid rgb(200, 38, 50);
          height: 30px;
    line-height: 30px;
    box-sizing: border-box;
    }
    .el-input__inner{
       height: 30px;
    line-height: 30px;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
    }
    .svg-container {
      vertical-align: middle;
      width: 30%;
      display: inline-block;
      text-align:right !important;
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .register-form {
      box-sizing:border-box;
      position: absolute;
      left: 0;
      right: 0;
      width: 376px;
      height:277px;
      padding: 0;
      margin: 0 auto;
      border:1px solid rgba(204, 204, 204, 1);
    }
    .el-form-item {
    margin-bottom: 10px;
}
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
    }
    .register-header{
      height:41px;
      line-height:41px;
          text-align: center;
          background-color: rgba(255, 51, 102, 1);
          padding:0 10px 0 15px;
          color:#fff;
    }
    .register-header>i{
      line-height:41px;
      color:#fff;
      font-weight:bolder;
    }
     .left{
       float:left;
     }
     .right{
       float:right;
     }
     .el-form-item__content>.el-button, .el-textarea__inner {
    font-size: 12px;
    padding:0;
    border:none;
}
.el-input.is-disabled .el-input__inner {
  padding:0 10px;
    background-color:transparent;
    border-color: transparent;
    color: #bbb;
    cursor: not-allowed;
}
.el-button--info {
    color: #666;
    background-color: #c4c4c4;
}
.el-button--info,.el-button--danger{
 
  width:40%;
}
    .thirdparty-button{
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
  }
</style>
